﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class XTJSettlement : MonoBehaviour
{

    EginUser user = EginUser.Instance;
    EginXTJ xtj= EginXTJ.Instance;
    public List<GameObject> XTJSettlementObj;
    public List<UISprite> avatarList;
    public List<UITexture> wxAvatarList;
    public List<UILabel> usernameList;
    public List<UILabel> uidList;

    //scroe
    public List<GameObject> Score;
    public List<GameObject> ScoreFather;
    public List<UISprite> jiajian;
    //rank
    public List<UISprite> rank1;
    public List<UISprite> rankother;

    //bg
    public List<UISprite> ownBg;
    public GameObject BackHall;
    void Awake()
    {

        UIEventListener.Get(BackHall).onClick = ButtonClick;
    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "BackHall")
        {
            EginUser.Instance.gameUserInfoList.Clear();        
            Application.LoadLevel(ScenePath.Hall);
        }
    }

    public void LoadInfo(List<PaiRank> ranks)
    {

        for (int i = 0; i < ranks.Count;i++ )
        {
            XTJSettlementObj[i].SetActive(true);
            LoadUserInfo(ranks,i);
        }
      
    }

    void LoadUserInfo(List<PaiRank> ranks,int i)
    {
        if(ranks[i].AvatarUrl != ""){
            WXAvatarManager._instance.GetWxAvatar(ranks[i].PlayerId,wxAvatarList[i],ranks[i].AvatarUrl);
            wxAvatarList[i].gameObject.SetActive(true);
        }
        else
        {
            avatarList[i].spriteName = ranks[i].AvatarId.ToString();
        }

        usernameList[i].text = ranks[i].Nickname;
        uidList[i].text = ranks[i].PlayerId.ToString();


        #region               
        long tempScore = ranks[i].Coin;

        string tempStr = "";
        Debug.Log(tempStr);
        float x = Score[i].transform.localPosition.x;
        float y = Score[i].transform.localPosition.y;
        if (tempScore >= 0)
        {
            jiajian[i].gameObject.SetActive(true);
            jiajian[i].spriteName = "plus_" + "jia";
            tempStr = user.ReturnMoneyForLong(ranks[i].Coin);
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score[i]);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather[i].transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 30;

                    }
                    else if (str == "万")
                    {
                        x += 50;
                        str = "wan";
                    }
                    else if (str == "亿")
                    {
                         x += 50;
                        str = "yi";
                    }
                    else
                    {
                        x += 40;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "plus_" + str;
            }
        }
        else
        {
            jiajian[i].gameObject.SetActive(true);
            jiajian[i].spriteName = "less_" + "jian";
            tempStr = user.ReturnMoneyForLong(long.Parse(ranks[i].Coin.ToString().Remove(0,1)));
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score[i]);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather[i].transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 30;
                    }
                    else if (str == "万")
                    {
                        x += 50;
                        str = "wan";
                    }
                    else if (str == "亿")
                    {
                        x += 50;
                        str = "yi";
                    }
                    else
                    {
                        x += 40;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "less_" + str;
            }
        }

        #endregion
        Debug.Log("gameOverUserInfo[i].TotalRank = " + ranks[i].Rank);
        int rank = ranks[i].Rank;
        if (rank == 1)
        {
            rank1[i].gameObject.SetActive(true);
        }
        else
        {
            rankother[i].gameObject.SetActive(true);
            rankother[i].spriteName = rank.ToString();
        }       
    }
}
